package com.example.psychologicalappointmentsystem.config.auth;

import java.util.Objects;

/**
 * 自定义threadlocal
 */
public class PsyThreadLocalUtil {
    public static final ThreadLocal myThreadLocal = new ThreadLocal();

    public static void set(MyThreadLocalModel myThreadLocalModel) {
        myThreadLocal.set(myThreadLocalModel);
    }

    public static void clear() {
        myThreadLocal.remove();
    }

    public static MyThreadLocalModel get() {
        if (Objects.isNull(myThreadLocal.get())) {
            // 当前线程应该是内部线程，自动注入管理员用户
            injectSystem();
        }
        return (MyThreadLocalModel) myThreadLocal.get();
    }

    /**
     * 为当前线程注入 admin账号
     */
    public static void injectAdmin() {
        MyThreadLocalModel admin = new MyThreadLocalModel();
        admin.setUserId(1L);
        myThreadLocal.set(admin);
    }

    /**
     * 为当前线程注入 平台自动操作账号=0
     */
    public static void injectSystem() {
        MyThreadLocalModel admin = new MyThreadLocalModel();
        admin.setUserId(0L);
        myThreadLocal.set(admin);
    }

    private static boolean isSuperSysAdmin(int userId) {
        //  可以是一些写死的特殊账号，留个后门
        return userId == 0;
    }

}
